package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.entityVo.RoleMenuVo;
import com.antdesignbackend.backend.service.RoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class layoutController {
    @Autowired
    RoleMenuService roleMenuService;

    /**
     * 根据角色id查询菜单，允许一个用户多个角色
     */
    @PostMapping("/menu")
    @ResponseBody
    public ReturnVO menu(@RequestBody RoleMenuVo roleMenuVo,HttpServletRequest request) {
        List<RoleMenuVo> menuVos=(ArrayList<RoleMenuVo>)request.getSession().getAttribute("data");
        ReturnVO returnVO = new ReturnVO();
        if (roleMenuVo != null) {
            if(menuVos != null){
                returnVO.setCode("200");
                returnVO.setData(menuVos);
            }else {
                QueryWrapper<RoleMenuVo> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("rm.RoleID", roleMenuVo.getRoleIds()).eq("activity", 1);
                List<RoleMenuVo> roleMenuVos = roleMenuService.selectRoleMenuList(queryWrapper, null);
                if (roleMenuVos.size() > 0) {
                    returnVO.setCode("200");
                    returnVO.setData(roleMenuVos);
                    request.getSession().setAttribute("data", roleMenuVos);
                } else
                    returnVO.setCode("503");
            }
        } else {
            returnVO.setCode("503");
        }
        return returnVO;
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("LoginName");
        request.getSession().removeAttribute("RoleID");
        request.getSession().removeAttribute("RoleCheckKey");
        request.getSession().removeAttribute("Key");
        return "redirect:/login";
    }
}
